package com.imyuanma.qingyun.ums.controller;

import com.imyuanma.qingyun.common.core.structure.tree.TreeBuilder;
import com.imyuanma.qingyun.common.factory.BaseDOBuilder;
import com.imyuanma.qingyun.common.model.request.WebRequest;
import com.imyuanma.qingyun.common.model.response.Page;
import com.imyuanma.qingyun.common.model.PageQuery;
import com.imyuanma.qingyun.common.model.response.Result;
import com.imyuanma.qingyun.common.util.AssertUtil;
import com.imyuanma.qingyun.common.util.CollectionUtil;
import com.imyuanma.qingyun.interfaces.monitor.annotation.Trace;
import com.imyuanma.qingyun.ums.model.UmsResource;
import com.imyuanma.qingyun.ums.service.IUmsResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 资源web
 *
 * @author YuanMaKeJi
 * @date 2022-10-08 11:59:54
 */
@RestController
@RequestMapping(value = "/ums/umsResource")
public class UmsResourceController {

    /**
     * 资源服务
     */
    @Autowired
    private IUmsResourceService umsResourceService;


    @Trace("查询资源树")
    @PostMapping("/getTree")
    public Result<List<UmsResource>> getTree(@RequestBody WebRequest<UmsResource> webRequest) {
        // 查询列表
        List<UmsResource> list = umsResourceService.getList(webRequest.getBody());
        if (CollectionUtil.isNotEmpty(list)) {
            Collections.sort(list);
        }
        // 树构造
        return Result.success(TreeBuilder.buildTree(list));
    }

    @Trace("查询父资源节点选择树")
    @PostMapping("/getParentSelectedTree")
    public Result<List<UmsResource>> getParentSelectedTree(@RequestBody WebRequest<UmsResource> webRequest) {
        // 查询列表
        List<UmsResource> list = umsResourceService.getList(webRequest.getBody());
        if (list == null) {
            list = new ArrayList<>();
        }
        list.add(UmsResource.buildVirtualRoot());
        if (CollectionUtil.isNotEmpty(list)) {
            Collections.sort(list);
        }
        // 树构造
        return Result.success(TreeBuilder.buildTree(list));
    }

    /**
     * 列表查询
     *
     * @param webRequest 查询条件
     * @return
     */
    @Trace("查询资源列表")
    @PostMapping("/getList")
    public Result<List<UmsResource>> getList(@RequestBody WebRequest<UmsResource> webRequest) {
        return Result.success(umsResourceService.getList(webRequest.getBody()));
    }

    /**
     * 分页查询
     *
     * @param webRequest 查询条件
     * @return
     */
    @Trace("分页查询资源")
    @PostMapping("/getPage")
    public Page<List<UmsResource>> getPage(@RequestBody WebRequest<UmsResource> webRequest) {
        PageQuery pageQuery = webRequest.buildPageQuery();
        List<UmsResource> list = umsResourceService.getPage(webRequest.getBody(), pageQuery);
        return Page.success(list, pageQuery);
    }

    /**
     * 统计数量
     *
     * @param webRequest 查询条件
     * @return
     */
    @Trace("统计资源数量")
    @PostMapping("/count")
    public Result<Integer> count(@RequestBody WebRequest<UmsResource> webRequest) {
        return Result.success(umsResourceService.count(webRequest.getBody()));
    }

    /**
     * 查询
     *
     * @param webRequest 参数
     * @return
     */
    @Trace("查询资源")
    @PostMapping("/get")
    public Result<UmsResource> get(@RequestBody WebRequest<Long> webRequest) {
        AssertUtil.notNull(webRequest.getBody());
        return Result.success(umsResourceService.get(webRequest.getBody()));
    }

    /**
     * 新增
     *
     * @param webRequest 参数
     * @return
     */
    @Trace("新增资源")
    @PostMapping("/insert")
    public Result insert(@RequestBody WebRequest<UmsResource> webRequest) {
        UmsResource umsResource = webRequest.getBody();
        AssertUtil.notNull(umsResource);
        BaseDOBuilder.fillBaseDOForInsert(umsResource);
        umsResourceService.insertSelective(umsResource);
        return Result.success();
    }

    /**
     * 修改
     *
     * @param webRequest 参数
     * @return
     */
    @Trace("修改资源")
    @PostMapping("/update")
    public Result update(@RequestBody WebRequest<UmsResource> webRequest) {
        UmsResource umsResource = webRequest.getBody();
        AssertUtil.notNull(umsResource);
        AssertUtil.notNull(umsResource.getId());
        BaseDOBuilder.fillBaseDOForUpdate(umsResource);
        umsResourceService.updateSelective(umsResource);
        return Result.success();
    }

    /**
     * 删除
     *
     * @param webRequest 参数, 待删除主键,多个使用英文逗号拼接
     * @return
     */
    @Trace("删除资源")
    @PostMapping("/delete")
    public Result delete(@RequestBody WebRequest<String> webRequest) {
        String ids = webRequest.getBody();
        AssertUtil.notBlank(ids);
        if (ids.contains(",")) {
            umsResourceService.batchDelete(Arrays.stream(ids.split(",")).map(Long::valueOf).collect(Collectors.toList()));
        } else {
            umsResourceService.delete(Long.valueOf(ids));
        }
        return Result.success();
    }

}
